package com.cloud.shopping.pay;

import com.cloud.shopping.order.model.Order;
import com.cloud.shopping.order.model.OrderRefund;
import com.cloud.shopping.tool.IpUtils;
import com.cloud.shopping.tool.Signature;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author John.zhang
 * @version Id: WeChatPayParam, v 1.0 2021/1/6 14:19 ZhiYong Exp $
 */
@Component
public class WeChatPayParam {

    @Resource
    private Signature signature;

    /*****
     * 支付退款数据处理
     */
    public String weChatRefundParam(OrderRefund orderRefund) throws Exception {
        //预支付下单需要用到的数据
        Map<String, String> data = new HashMap<>(16);
        //订单号
        data.put("out_trade_no", orderRefund.getOrderNo());
        //退款订单号
        data.put("out_refund_no", orderRefund.getId());
        //支付金额
        //data.put("total_fee",String.valueOf(orderRefund.getMoney()));
        //退款金额
        data.put("refund_fee", "1");
        //支付金额
        data.put("total_fee", "1");
        //回调地址（退款申请结果通知地址）
        data.put("notify_url", "http://youngeco.site:59482/wx/refund/result");
        // TreeMap->MD5->Map->JSON->AES
        return signature.security(data);
    }


    /*****
     * 支付数据处理
     */
    public String weChatParam(Order order, HttpServletRequest request) throws Exception {
        //预支付下单需要用到的数据
        Map<String, String> data = new HashMap<>(16);
        data.put("body", "cloud-shopping云商城");
        //订单号
        data.put("out_trade_no", order.getId());
        data.put("device_info", "PC");
        // 币种
        data.put("fee_type", "CNY");
        //支付金额
        //data.put("total_fee", String.valueOf(order.getMoneys()*100));
        //支付金额
        data.put("total_fee", "1");
        //客户端IP
        data.put("client_ip", IpUtils.getIpAddr(request));
        //回调地址（支付结果通知地址）
        data.put("notify_url", "http://youngeco.site:59482/wx/result");
        // 此处指定为扫码支付
        data.put("trade_type", "NATIVE");

        // TreeMap->MD5->Map->JSON->AES
        return signature.security(data);
    }
}
